package com.honghe.controller;

import com.honghe.constant.JwtClaimsConstant;
import com.honghe.dto.*;
import com.honghe.entity.Doctor;
import com.honghe.exception.BaseException;
import com.honghe.properties.JwtProperties;
import com.honghe.result.PageResult;
import com.honghe.result.Result;
import com.honghe.service.DoctorService;
import com.honghe.utils.JwtUtil;
import com.honghe.vo.DocVisitVO;
import com.honghe.vo.DoctorLoginVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


@RestController
@RequestMapping("/doctor/doctor")
@Tag(name="医生相关接口")
public class DoctorController {
    @Autowired
    private DoctorService doctorService;
    @Autowired
    private JwtProperties jwtProperties;

    @PostMapping("/login")
    @Operation(summary="医生登录")
    public Result<DoctorLoginVO> login(@RequestBody DoctorLoginDTO doctorLoginDTO){
        Doctor doctor = doctorService.login(doctorLoginDTO);
        Map<String, Object> claims=new HashMap<>();
        claims.put(JwtClaimsConstant.DOC_ID,doctor.getId());
        claims.put(JwtClaimsConstant.DOC_JOB,doctor.getJob());
        //创建token
        String token = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), claims);
        DoctorLoginVO doctorLoginVO=new DoctorLoginVO(
                doctor.getId(),
                doctor.getUsername(),
                doctor.getName(),
                token);
        return Result.success(doctorLoginVO);
    }
    @GetMapping("/page")
    @Operation(summary="分页查询医生")
    public Result<PageResult> pageQuery(DoctorPageDTO doctorPageDTO){
        PageResult pageResult=doctorService.pageQuery(doctorPageDTO);
        return Result.success(pageResult);
    }
    @PostMapping("/add")
    @Operation(summary="新增医生")
    public Result<String> insert(@RequestBody DoctorDTO doctorDTO){
        doctorService.insert(doctorDTO);
        return Result.success();
    }
    @PutMapping("/update")
    @Operation(summary = "更新医生信息")
    @CacheEvict(cacheNames = "doctor",key = "#doctorDTO.deptId")
    public Result<String> update(@RequestBody DoctorDTO doctorDTO){
        doctorService.update(doctorDTO);
        return Result.success();
    }
    @DeleteMapping("/delete")
    @Operation(summary = "删除医生")
    @CacheEvict(cacheNames = "doctor",key = "#doctorDTO.deptId")
    public Result<String> delete(@RequestParam Long id){
        doctorService.delete(id);
        return Result.success();
    }
    @GetMapping("/get/{id}")
    @Operation(summary = "根据id查询医生")
    public Result<Doctor> getById(@PathVariable Long id){
        Doctor doctor=doctorService.getById(id);
        doctor.setPassword("******");
        return Result.success(doctor);
    }
    @PutMapping("/change")
    @Operation(summary = "修改密码")
    public Result<String> changePassword(@RequestBody DoctorPasswordDTO doctorPasswordDTO){
        doctorService.changePassword(doctorPasswordDTO);
        return Result.success("修改成功");
    }
    @GetMapping("/visit")
    @Operation(summary = "获取对应医生下所挂号病人的信息")
    public Result<DocVisitVO> getPatByDate(@DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate date){
        DocVisitVO docVisitVO=doctorService.getPatByDate(date);
        return Result.success(docVisitVO);
    }
    @PutMapping("/status")
    @Operation(summary = "更新挂号信息状态")
    public Result<String> updateStatus(@RequestBody DocUpdateStatusDTO docUpdateStatusDTO){
        doctorService.updateStatus(docUpdateStatusDTO);
        return Result.success("更新成功");
    }
    @PostMapping("/import")
    @Operation(summary = "上传excel文件批量插入")
    public Result<String> insertByExcel(@RequestParam("file") MultipartFile file) throws IOException {
        String filePath=upload(file);
        doctorService.importExcel(filePath);
        return Result.success("添加成功");
    }
    public String upload(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        int i = originalFilename.lastIndexOf(".");
        String fileSuf = originalFilename.substring(i);
        if (!fileSuf.equals(".xlsx")){
            throw new BaseException("文件格式错误");
        }
        String filePath = "D:\\javaweb_temp\\hoh-registered\\hos-server\\src\\main\\resources\\temp\\"+ UUID.randomUUID()+fileSuf;
        file.transferTo(new File(filePath));
        return filePath;
    }
}
